/*
 ******************
 * author: 马丰彦
 * date: 2019-05-08
 * function: 路由配置表
 ******************
 */

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * hidden: true                   如果为true，侧边栏显示，否则是为通用隐藏路由
 * alwaysShow: true               如果为true，将总显示
 *                                如果设置了,路由数量必须大于1
 * redirect: noRedirect           如果设置noRedirect，面包屑不重定向标题
 * name:'router-name'             路由名字必须填写，keep-live使用
 * meta : {
    roles: ['admin']              路由权限唯一标识，在queryUserPower请求接口比对递归查询(roles 一定要为数组！)
    title: 'title'                路由标题，面包屑展示标题
    icon: 'svg-name'              路由图标
    breadcrumb: false             如果为false，面包屑标题隐藏
  }
 */

/**
 * constantRoutes
 * 基本路由，不需要权限筛查
 * 所有路由都将被展示
 */
export const constantRoutes = [
  // 登录
  /* {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  }, */
  // 404报错

  // 默认重定向主页
  {
    path: '/',
    component: Layout,
    hidden: true,
    redirect: '/dashboard/index'
    /* children: [{
      path: 'index',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {
        title: '概要数据',
        icon: 'dashboard'
      }
    }] */
  },
  {
    path: '/dashboard',
    component: Layout,
    name: 'dashboard',
    children: [{
      path: 'index',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index')
      /* meta: {
        title: '服务管理',
        icon: 'servicesss'
      } */
    }]
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 无限嵌套例子
  {
    path: '/nested',
    hidden: true,
    component: Layout,
    // redirect: '/nested/menu1',
    name: 'Nested',
    meta: {
      title: 'Nested',
      icon: 'nested'
    },
    children: [{
      path: 'menu1',
      component: () => import('@/views/nested/menu1/index'), // Parent router-view
      name: 'Menu1',
      meta: {
        title: 'Menu1'
      },
      children: [{
        path: 'menu1-1',
        component: () => import('@/views/nested/menu1/menu1-1'),
        name: 'Menu1-1',
        meta: {
          title: 'Menu1-1'
        }
      },
      {
        path: 'menu1-2',
        component: () => import('@/views/nested/menu1/menu1-2'),
        name: 'Menu1-2',
        meta: {
          title: 'Menu1-2'
        },
        children: [{
          path: 'menu1-2-1',
          component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
          name: 'Menu1-2-1',
          meta: {
            title: 'Menu1-2-1'
          }
        },
        {
          path: 'menu1-2-2',
          component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
          name: 'Menu1-2-2',
          meta: {
            title: 'Menu1-2-2'
          }
        }
        ]
      },
      {
        path: 'menu1-3',
        component: () => import('@/views/nested/menu1/menu1-3'),
        name: 'Menu1-3',
        meta: {
          title: 'Menu1-3'
        }
      }
      ]
    },
    {
      path: 'menu2',
      component: () => import('@/views/nested/menu2/index'),
      meta: {
        title: 'menu2'
      }
    }
    ]
  },
  // 产品组管理
  {
    path: '/productGroup',
    component: Layout,
    name: 'productGroup',
    meta: {
      roles: ['business-product']
    },
    children: [{
      path: 'index',
      name: 'ProductGroup',
      component: () => import('@/views/productGroup/index'),
      meta: {
        title: '产品组管理',
        icon: 'productGroup'
      }
    }]
  },
  // 产品组审核管理
  {
    path: '/groupCheck',
    component: Layout,
    name: 'GroupCheck',
    meta: {
      title: '产品组审核',
      icon: 'check'
    },
    children: [{
      path: 'groupCheck',
      name: 'groupCheck',
      component: () => import('@/views/groupCheck/index'),
      meta: {
        roles: ['business-checkProduct'],
        title: '产品组审核',
        icon: 'shenhe'
      }
    }]
  },
  // 支持外链形式菜单
  {
    path: 'external-link',
    hidden: true,
    component: Layout,
    children: [{
      path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
      meta: {
        title: 'External Link',
        icon: 'link'
      }
    }]
  }
]

/**
 * asyncRoutes
 * 所有路由需要被权限递归筛查动态获取
 * 再次强调，roles一定要为数组类型！
 */
export const asyncRoutes = [

  // 服务管理
  /* {
    path: '/service',
    component: Layout,
    name: 'service',
    meta: {
      roles: ['business-service']
    },
    children: [{
      path: 'index',
      name: 'service',
      component: () => import('@/views/service/index'),
      meta: {
        title: '服务管理',
        icon: 'user'
      }
    }]
  }, */

  // 服务管理business-service
  {
    path: '/service',
    component: Layout,
    name: 'service',
    meta: {
      roles: ['business-service']
    },
    children: [{
      path: 'index',
      name: 'Service',
      component: () => import('@/views/service/index'),
      meta: {
        title: '服务管理',
        icon: 'servicesss'
      }
    }]
  },

  // 产品管理
  {
    path: '/product',
    component: Layout,
    name: 'product',
    meta: {
      roles: ['business-product']
    },
    children: [{
      path: 'index',
      name: 'Product',
      component: () => import('@/views/product/index'),
      meta: {
        title: '产品管理',
        icon: 'myProduct'
      }
    }]
  },

  // 审核管理
  {
    path: '/check',
    component: Layout,
    name: 'checkmanager',
    redirect: '/check/productcheck',
    meta: {
      roles: ['business-checkProduct'],
      title: '审核管理',
      icon: 'check'
    },
    children: [{
      path: 'productcheck',
      name: 'productcheck',
      component: () => import('@/views/check/productcheck/index'),
      meta: {
        roles: ['business-checkProduct'],
        title: '产品审核',
        icon: 'shenhe'
      }
    }, {
      path: 'combocheck',
      name: 'combocheck',
      hidden: true,
      component: () => import('@/views/check/combocheck/index'),
      meta: {
        title: '套餐审核',
        icon: 'combocheck'
      }
    }]
  },

  // 订购管理
  {
    path: '/buy',
    component: Layout,
    name: 'buy',
    redirect: '/buy/buylist',
    meta: {
      roles: ['business-orderTable', 'business-accountTable', 'business-cancelOrderTable'],
      title: '订购管理',
      icon: 'product'
    },
    children: [{
      path: 'buylist',
      name: 'Buylist',
      component: () => import('@/views/buy/buylist/index'),
      meta: {
        title: '订单列表',
        roles: ['business-orderTable'],
        icon: 'buylist'
      }
    }, {
      path: 'billlist',
      name: 'billlist',
      component: () => import('@/views/buy/billlist/index'),
      meta: {
        title: '账单列表',
        roles: ['business-accountTable'],
        icon: 'billlist'
      }
    }, {
      path: 'unsubscribelist',
      name: 'unsubscribelist',
      component: () => import('@/views/buy/unsubscribelist/index'),
      meta: {
        title: '退订列表',
        roles: ['business-cancelOrderTable'],
        icon: 'unsubscribelist'
      }
    }]
  },

  // 资费策略
  {
    path: '/expenses',
    component: Layout,
    name: 'Expenses',
    meta: {
      roles: ['business-tactics']
    },
    children: [{
      path: 'index',
      name: 'expenses',
      component: () => import('@/views/expenses/index'),
      meta: {
        roles: ['business-tactics'],
        title: '计费策略管理',
        icon: 'jifei'
      }
    }]
  },
  // 支付渠道管理
  {
    path: '/payChannel',
    component: Layout,
    name: 'PayChannel',
    meta: {
      roles: ['business-channelManage']
    },
    children: [{
      path: 'index',
      name: 'payChannel',
      component: () => import('@/views/payChannel/index'),
      meta: {
        // roles: ['business-tactics'],
        title: '支付渠道管理',
        icon: 'jifei'
      }
    }]
  },

  /* // 套餐管理
  {
    path: '/combo',
    component: Layout,
    name: 'combo',
    meta: {
      roles: ['business-combo']
    },
    children: [{
      path: 'index',
      name: 'combo',
      component: () => import('@/views/combo/index'),
      meta: {
        title: '套餐管理',
        icon: 'user'
      }
    }]
  }, */

  // 策略管理
  {
    path: '/discounts',
    component: Layout,
    name: 'discounts',
    children: [{
      path: 'index',
      name: 'Discounts',
      component: () => import('@/views/discounts/index'),
      meta: {
        title: '优惠策略管理',
        roles: ['business-preferTariff'],
        icon: 'expenses'
      }
    }]
  },

  /* // 虚拟券管理
  {
    path: '/virtual',
    component: Layout,
    name: 'virtual',
    meta: {
      roles: ['business-virtual']
    },
    children: [{
      path: 'index',
      name: 'virtual',
      component: () => import('@/views/virtual/index'),
      meta: {
        title: '套餐管理',
        icon: 'user'
      }
    }]
  }, */

  // 重定向
  {
    path: '*',
    redirect: '/404',
    hidden: true,
    component: () => import('@/views/404')
  }
]

const createRouter = () => new Router({
  // mode: 'history', // 服务端渲染
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 重定向路由
}

export default router
